<?php

namespace slinks\extensions\action\core;

use slinks\extensions\action\core\IActionContext;
use slinks\core\DependencyInjection\ContainerInterface;
use slinks\extensions\action\core\IActionProxy;

class ContainerActionProxy implements IActionProxy {
    /* @var $container ContainerInterface */

    private $container;

    /* @var $actionContext IActionContext */
    private $actionContext;

    function __construct(ContainerInterface $container, IActionContext $actionContext) {
        $this->container = $container;
        $this->actionContext = $actionContext;
    }

    /**
     * @return mixed instance of the action object.
     */
    public function getObject() {
        return $this->container->get($this->actionContext->getActionMapping()->getClassName());
    }

    /**
     * Execute the actual method for the object proxied by this proxy.
     * @return string return code from the action.
     */
    public function execute() {
        // it is possible that the method name is a wildcard if so then lookup name 
        // from valuestack 
        $methodName = $this->actionContext->getActionMapping()->getMethodName();
        if($methodName == ''){
            $methodName = $this->actionContext->getValueStack()->get('_actionMethod');
        }
        return call_user_func(array($this->getObject(), $methodName));
    }

}